package com.study.controller;

import com.study.common.Result;
import com.study.common.ResultCode;
import com.study.entity.atte.ArchiveMonthly;
import com.study.entity.atte.ArchiveMonthlyInfo;
import com.study.entity.atte.Attendance;
import com.study.service.ArchiveService;
import com.study.service.AtteService;
import com.study.service.ExcelImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @Author wfw
 * @Date 2020/08/01 21:58
 */
@RestController
@RequestMapping("attendances")
public class AttendanceController extends BaseController {

    @Autowired
    private ExcelImportService excelImportService;

    @Autowired
    private AtteService atteService;

    @Autowired
    private ArchiveService archiveService;

    /**
     * 上传考勤数据
     */
    @PostMapping("import")
    public Result importExcel(@RequestParam(name ="file") MultipartFile file) throws Exception {
        excelImportService.importAttendanceExcel(file , companyId);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 查询考勤数据列表
     */
    @GetMapping("")
    public Result list(int page,int pagesize) throws ParseException {
        Map map = atteService.getAtteDate(companyId , page , pagesize);
        return new Result(ResultCode.SUCCESS , map);
    }

    /**
     * 编辑用户的考勤记录
     */
    @PutMapping("{id}")
    public Result editAtte(@RequestBody Attendance attendance){
        atteService.editAtte(attendance);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 获取月度报表归档数据
     */
    @GetMapping("reports")
    public Result reports(String atteDate){
        List<ArchiveMonthlyInfo> list = atteService.getReports(atteDate , companyId);
        return new Result(ResultCode.SUCCESS , list);
    }

    /**
     * 数据归档
     */
    @GetMapping("archive/item")
    public Result archive(String archiveDate) {
        archiveService.saveArchive(archiveDate , companyId);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 新建报表
     */
    @GetMapping("newReports")
    public Result newReports(String atteDate) {
        atteService.newReports(atteDate , companyId);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 归档历史列表
     */
    @GetMapping("reports/year")
    public Result findReportsYear(String year) {
        List<ArchiveMonthly> list = archiveService.findByYear(year , companyId);
        return new Result(ResultCode.SUCCESS , list);
    }

    /**
     * 查询归档详情
     */
    @PostMapping("reports/{id}")
    public Result findInfosById(@PathVariable String id) {
        List<ArchiveMonthlyInfo> list = archiveService.findMonthInfoByAmid(id);
        return new Result(ResultCode.SUCCESS , list);
    }

    /**
     * 根据用户id和月份查询已归档的考勤明细
     */
    @GetMapping("archive/{userId}/{yearMonth}")
    public Result historyData(@PathVariable String userId,@PathVariable String yearMonth) {
        ArchiveMonthlyInfo archiveMonthlyInfo = archiveService.findUserArchiveDetail(userId , yearMonth);
        return new Result(ResultCode.SUCCESS , archiveMonthlyInfo);
    }

}
